<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }
class CategoryController Extends BaseController
{
	private $page = 1;
	private $cid = 1;
	private $numPageShow = '';
	
  public function index($cid = 0, $page = 1) {  		  	            	   
  	  
  	//echo "page = ".$page;
  	$this->page = $page;
  	$this->cid = $cid;

  	//get book number of one special page
  	$num = $this->registry->db->get('configManager')->getById(2)->get("value");
  	$numOfBook =  $this->registry->db->get('bookManager')->getNumberOfCategoryBook($cid);
  	$showedBooks = $this->registry->db->get('bookManager')->getCategoryBooks($cid, ($this->page-1)*$num, $num);
  	$catName = $this->registry->db->get('catManager')->get('cid', $this->cid)->get('name');
    $catBooksContent = '<div class="crumb_nav">
			            <a href="../index">Home</a> &gt;&gt; '.$catName.
			            '</div>
    					<div class="title"><span class="title_icon"><img src="{bstoViewPath}images/bullet1.gif" alt="" title="" /></span>'.ucfirst($catName).' books</div>
    					<div class="new_products">';    	 	    
  	$catBooksContent =  $catBooksContent.$this->registry->template->getCategoryBooksContent($showedBooks, $this->showPagingBar($numOfBook, $num)).'</div>';		  	
    
    $this->registry->template->setMenuKey('category');
  	$tags = array('bstoHead'=>$headContent);
    	$user = $this->registry->db->get('userManager')->getCurrentUser();
        if($user != null){
        	$tags['login'] = '<li id="login"><span> Hello, '. $user->get('username').'</span><a href="{bstoRootPath}logout">Logout</a></li>';	
        }else{
        	$tags['login'] = '<li id="login"><a href="{bstoRootPath}login">Login</a></li>';
        }
    $output = $this->registry->template->getPageContent('index', 'Books', $catBooksContent, $tags);
    				                      
    echo $output;      
  }   
  
   public function showPagingBar( $numOfBook, $num){
        $numOfPage = ceil($numOfBook / $num);
        $str = '';
        if($numOfBook == 0 || $numOfPage==1){
            $str .= '<div class="pagination"> </div>';
        }else {
            if($numOfPage == 1){
                $str ='<div class="pagination"> <span class="disabled">{First}</span> ';
                $str .='<span class="disabled">&lt;&lt;</span> ';
                $str .= '<a href="{bstoRootPath}category/' .$this->cid. '/1"><span class="current">1</span></a>';
                $str .= '<span class="disabled"> &gt;&gt; </span>';
                $str .= '<span class="disabled"> {Last} </span>';
                $str .= '</div>';
            }else if ($this->page == 1){
                $str ='<div class="pagination"> <span class="disabled">{First}</span> ';
                $str .='<span class="disabled">&lt;&lt;</span> ';
                $i = 0;
                for($i=$this->page ; $i < $this->page + 3; $i++){
                    if($i <= $numOfPage){
                        if($i == $this->page){
                            $str .= '<a href="{bstoRootPath}category/' .$this->cid. '/' .$i. '"><span class="current">' .$i. '</span></a>';
                        }else{
                            $str .= '<a href="{bstoRootPath}category/' .$this->cid. '/' .$i. '">' .$i. '</a>';    
                        }
                            
                    }            
                }
                if($i < $numOfPage){
                    $str .= "...";
                }
                $next = $this->page + 1;
                $str .= '<a href="{bstoRootPath}category/' .$this->cid. '/' .$next . '"> &gt;&gt; </a>';
                $str .= '<a href="{bstoRootPath}category/' .$this->cid. '/' .$numOfPage . '"> {Last} </a>';
                $str .= '</div>';
        		            
            } else if ($this->page == $numOfPage){
                $previous = $this->page - 1;
                $str ='<div class="pagination"> <a href="{bstoRootPath}category/' .$this->cid. '/1">{First}</a>';
                $str .='<a href="{bstoRootPath}category/' .$this->cid. '/' .$previous.'">&lt;&lt;</a>';
                $i = $this->page - 2;
                if($i > 1){
                    $str .= "...";
                }
                for($i=$this->page - 2; $i <= $numOfPage; $i++){
                    if($i >= 1){
                        if($i == $this->page){
                            $str .= '<a href="{bstoRootPath}category/' .$this->cid. '/' .$i. '"><span class="current">' .$i. '</span></a>';
                        }else{
                            $str .= '<a href="{bstoRootPath}category/' .$this->cid. '/' .$i. '">' .$i. '</a>';    
                        }                    
                    }            
                }    
                $str .= '<span class="disabled"> &gt;&gt; </span>';
                $str .= '<span class="disabled"> {Last} </span>';
                $str .= '</div>';
                       
            } else {
                $previous = $this->page - 1;
                $next = $this->page + 1;
                $str .='<div class="pagination"> <a href="{bstoRootPath}category/' .$this->cid. '/1">{First}</a>';
                $str .='<a href="{bstoRootPath}category/' .$this->cid. '/' .$previous. '">&lt;&lt;</a>';
                $i = $this->page;
                for($i = $this->page -1; $i < $this->page + 2; $i++){
                    if($i <= $numOfPage){
                        if($i == $this->page ){
                            $str .= '<a href="{bstoRootPath}category/' .$this->cid. '/' .$i. '"><span class="current">' .$i. '</span></a>';
                        }else{
                            $str .= '<a href="{bstoRootPath}category/' .$this->cid. '/' .$i. '">' .$i. '</a>';    
                        }    
                    }                    
                }
                if($i < $numOfPage){
                    $str .= "...";
                }
                $str .= '<a href="{bstoRootPath}category/' .$this->cid. '/' .$next. '"> &gt;&gt; </a>';
                $str .= '<a href="{bstoRootPath}category/' .$this->cid. '/' .$numOfPage . '"> {Last} </a>';
                $str .= '</div>'; 
                     
            }
        }
        
        return $str;   
   }
}
?>